package com.hopu.service.impl;

import com.hopu.entity.User;
import com.hopu.mapper.UserMapper;
import com.hopu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 新增用户
     * @param user
     * @return
     */
    @Override
    public void save(User user) {
        user.setCreateTime(LocalDateTime.now());
        //加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        userMapper.insert(user);
    }

    /**
     * 通过username和password查询用户
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        //加密
       user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        return userMapper.getByUsernameAndPassword(user);
    }

    // 更新用户资料
    @Override
    public User updateUser(User user){
        userMapper.updateUserInfo(user);
        return userMapper.getById(String.valueOf(user.getUserId()));
    }

    @Override
    public User updateUserInfo(User user) {
        return null;
    }
}
